class Solution {
public:
    void swap(vector<int>& nums, int i, int j)
    {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
    void f(vector<int>& nums, int i, vector<vector<int>>& ans)
    {
        if (i == nums.size())
        {
            vector<int> cur;
            for (int num : nums)
            {
                cur.push_back(num);
            }
            ans.push_back(cur);
            return;
        }
        else
        {
            for (int j = i; j < nums.size(); j++)
            {
                swap(nums, i, j);
                f(nums, i + 1, ans);
                swap(nums, i, j);
            }
        }
    }

    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> ans;
        f(nums, 0, ans);
        return ans;
    }

};